<template>
  <div>
    <!--    添加用户-->
    <div id="form">
      <el-form label-width="280px">
        <el-form-item label="用户名">
          <el-input v-model="tableData.username" placeholder="请输入用户名" style="width:100% "></el-input>
        </el-form-item>
        <el-form-item label="密码" style="margin-top: 50px">
          <el-input type="textarea" v-model="tableData.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="用户所属角色">
          <el-select v-model="tableData.roleId" clearable placeholder="请选择用户所属角色">
            <el-option
              v-for="item in RoleList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-top: 50px;">
          <el-button style="margin-right: 30px" type="primary" @click="onSubmit">立即添加</el-button>
          <el-button type="info" @click="onClean">重新输入</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import userApi from "@/api/user";
import roleApi from '@/api/role'
import qs from "qs";

export default {
  name: "add",
  data() {
    return {
      tableData: {
        username: "",
        password: "",
        roleId:''
      },
      RoleList:[]
    }
  },
  methods: {
    //点击添加
    onSubmit() {
      userApi.addUser(qs.stringify(this.tableData)).then(resp => {
        if (resp.code === 6666) {
          this.$message.success("添加成功！");
          this.onClean();
          this.$router.push('/rbac/user/list')
        } else {
          this.$message.info(resp.msg)
        }
      }).catch(() => {
        this.$message.error("添加出错！")
      })
    },
    //清楚已输入的数据
    onClean() {
      this.tableData = {}
    },
    //查询所有角色信息
    selectAllRoleList(){
      roleApi.selectAllRole().then(resp=>{
        if (resp.code===6666){
          this.RoleList=resp.data
        }else {
          this.$message.warning('查询角色信息失败')
        }
      }).catch(()=>{
        this.$message.error('查询角色信息出错')
      })
    }
  },
  mounted() {
    this.selectAllRoleList();
  }
}
</script>

<style scoped>
#form {
  margin-top: 120px;
  width: 680px;
}
</style>
